package com.basic.emg.service.impl;


import com.basic.common.exception.business.BusinessException;
import com.basic.common.exception.business.ValidationException;
import com.basic.emg.domain.*;
import com.basic.emg.mapper.EmergencyDisposeMapper;
import com.basic.emg.mapper.EmergencyMaterialMapper;
import com.basic.emg.service.EmergencyDisposeService;
import com.basic.emg.service.EmergencyMaterialService;
import com.basic.emg.utils.FileUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class EmergencyDisposeServiceImpl implements EmergencyDisposeService {

    @Autowired
    private EmergencyDisposeMapper disposeMapper;
    @Value("${uploadPath}")
    private String uploadPath;


    @Override
    public void createDispose(EmergencyDispose dispose, MultipartFile[] file) throws Exception  {
        if(StringUtils.isBlank(dispose.getEventNature())){
            throw new RuntimeException("事件性质不能为空");
        }
        if(StringUtils.isBlank(dispose.getStartPlan())){
            throw new RuntimeException("应急预案不能为空");
        }

        dispose.setCreateTime(new Date());
        dispose.setUpdateTime(new Date());
        dispose.setDelFlag("0");

        List pathList = new ArrayList();

        if(file!=null && file.length>0){
            for(MultipartFile f : file){
                //上传文件
                String path = FileUtil.uploadMeterImg(uploadPath + "dispose", f);
                if(StringUtils.isNotBlank(path)){
                    pathList.add(path);
                }
            }
            String join = StringUtils.join(pathList, ",");
            join = join.replaceAll("\\\\", "/");
            System.out.println(join);
            dispose.setFilePath(join);
        }

        int i = disposeMapper.insertDispose(dispose);

    }

    @Override
    public List<EmergencyDispose> selectDisposeList(EmergencyDispose disposel) {
        Example example = new Example(EmergencyDispose.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(disposel.getStartPlan())){
            criteria.andEqualTo("startPlan",disposel.getStartPlan());
        }
        if(disposel.getStartDate() != null){
            criteria.andEqualTo("startDate", disposel.getStartDate());
        }
        if(disposel.getStartTime1()!=null){
            criteria.andGreaterThanOrEqualTo("updateTime",disposel.getStartTime1());
        }
        if(disposel.getEndTime1()!=null){
            criteria.andLessThanOrEqualTo("updateTime",disposel.getEndTime1());
        }
        if(disposel.getStartTime2()!=null){
            criteria.andGreaterThanOrEqualTo("startDate",disposel.getStartTime2());
        }
        if(disposel.getEndTime2()!=null){
            criteria.andLessThanOrEqualTo("startDate",disposel.getEndTime2());
        }
        criteria.andNotEqualTo("delFlag","1");
        List<EmergencyDispose> emergencyDisposes = disposeMapper.selectByExample(example);
        return emergencyDisposes;
    }

    @Override
    public EmergencyDispose selectDisposeById(Long id) {
        EmergencyDispose dispose = disposeMapper.selectByPrimaryKey(id);
        if (dispose != null && dispose.getDelFlag().equals("1")){
            return null;
        }
        return dispose;
    }

    @Transactional
    @Override
    public void editDispose(EmergencyDispose dispose,MultipartFile[] file) throws Exception{

        List pathList = new ArrayList();

        if(file!=null && file.length>0){
            for(MultipartFile f : file){
                //上传文件
                String path = FileUtil.uploadMeterImg(uploadPath + "dispose", f);
                if(StringUtils.isNotBlank(path)){
                    pathList.add(path);
                }
            }
            String join = StringUtils.join(pathList, ",");
            System.out.println(join);
            if(StringUtils.isNotBlank(dispose.getFilePath())){
                String old_filePath = dispose.getFilePath();
                join = old_filePath + "," + join;
                join = join.replaceAll("\\\\", "/");
            }
            dispose.setFilePath(join);
        }
        dispose.setUpdateTime(new Date());
        int i = disposeMapper.updateByPrimaryKeySelective(dispose);
        if (i != 1){
            throw new RuntimeException("更新失败！");
        }
    }

    @Transactional
    @Override
    public void deleteDispose(List<Long> ids) {

        if(CollectionUtils.isEmpty(ids)){
            throw new RuntimeException("id不能为空");
        }
        for(Long id : ids){
            EmergencyDispose dispose = new EmergencyDispose();
            dispose.setId(id);
            dispose.setDelFlag("1");
            disposeMapper.updateByPrimaryKeySelective(dispose);
        }

    }

}
